package mspbots.data.tl.prod.autotask.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskContractServiceBundleUnitsMapper;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskContractServiceBundleUnits;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskContractServiceBundleUnitsService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

@Service
@Log4j2
@RequiredArgsConstructor
@DS("master")
public class TaskAutotaskContractServiceBundleUnitsServiceImpl extends AbstractBaseServiceImpl<TaskAutotaskContractServiceBundleUnitsMapper, TaskAutotaskContractServiceBundleUnits>
        implements TaskAutotaskContractServiceBundleUnitsService {

    @Override
    public void sync(ResultEntity content) {
        log.debug("Auto task contractServiceBundleUnits receiver content {}", content);
        JsonNode entity = content.getEntity();
        TaskAutotaskContractServiceBundleUnits data = TaskAutotaskContractServiceBundleUnits.builder()
                .contractServiceBundleUnitsId(super.getLongValue(entity.get("id")))
                .contractServiceBundleId(super.getIntegerValue(entity.get("contractServiceBundleID")))
                .contractId(super.getIntegerValue(entity.get("contractID")))
                .organizationalLevelAssociationId(super.getIntegerValue(entity.get("organizationalLevelAssociationID")))
                .serviceBundleId(super.getIntegerValue(entity.get("serviceBundleID")))
                .units(super.getIntegerValue(entity.get("units")))
                .approveAndPostDate(super.getDateTimeValue(entity.get("approveAndPostDate")))
                .endDate(super.getDateTimeValue(entity.get("endDate")))
                .startDate(super.getDateTimeValue(entity.get("startDate")))
                .cost(super.getDoubleValue(entity.get("cost")))
                .internalCurrencyPrice(super.getDoubleValue(entity.get("internalCurrencyPrice")))
                .price(super.getDoubleValue(entity.get("price")))
                .syncTime(LocalDateTime.now())
                .build();
                data.setTenantCode(content.getTenantId());

        TaskAutotaskContractServiceBundleUnits dbData = super.baseMapper.selectOne(Wrappers.<TaskAutotaskContractServiceBundleUnits>lambdaQuery()
                        .eq(TaskAutotaskContractServiceBundleUnits::getTenantCode, data.getTenantCode())
                        .eq(TaskAutotaskContractServiceBundleUnits::getContractServiceBundleUnitsId, data.getContractServiceBundleUnitsId())
                        .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)) {
            super.baseMapper.insert(data);
        } else {
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }
    }

    }

